<?php

namespace App\Models;

use App\Http\Common\Constant;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\StoreCash
 *
 * @property int $id
 * @property int $store_id 商家店铺id
 * @property int $user_id 财务操作员
 * @property float $money 提现总金额
 * @property float $proportion 提现比例 = 100% - 商家提现手续费比例
 * @property int $status 提现状态，0失败，1申请提现，2提现完成
 * @property string|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereProportion($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereStoreId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\StoreCash whereUserId($value)
 * @mixin \Eloquent
 */
class StoreCash extends Model
{
    protected $table="store_cash";

    public function cash($store_id, $money, $user_id)
    {
        // todo 商家提现
        $this->store_id = $store_id;
        $this->money = $money;
        $this->proportion = Constant::STORE_INCOME_PROPORTION;
        $this->user_id = $user_id;
        if ($this->save())
        {
            throw new \Exception("体现记录失败");
        }


    }

    // todo 提现手续费

    public static function serviceCharge($stroe_chsh_id)
    {
        //查询商家提现日志记录
        $store_cash = StoreCash::whereId($stroe_chsh_id)->first();
        //查询商家
        $store = Store::whereId($store_cash->store_id)->first();
        $money =    floor($store_cash->money * $store_cash->proportion); // 商家提现总金额的0.1
        // 判断体现金额
        if ($store->usable_money < $money){
            throw new \Exception("提现金额超出，可提现余额！");
        }

        //todo 省代理 20%
        $province_agent = ProvinceAgent::whereId($store->province_code)->first();
        if (isset($province_agent)){
            $province_agent_money  = floor($money * Constant::STORE_PROVINCE_AGENT);
            $province_agent->increment("useable_money",$province_agent_money);
            $province_agent->increment("total_money",$province_agent_money);
            //省代理日志
            $province_agent_income_log = new ProvinceAgentIncomeLog();
            $province_agent_income_log->province_agent_id = $province_agent->id;
            $province_agent_income_log->store_cash_id = $store_cash->id;
            $province_agent_income_log->store_id= $store->id;
            $province_agent_income_log->money = $province_agent_money;
            $province_agent_income_log->proportion = Constant::STORE_CITY_AGENT;
            $province_agent_income_log->save();

        }
        //todo 市级代理 30%
        $city_agent = CityAgent::whereId($store->city_code)->first();
        if (isset($city_agent)){
            $city_agent_money = floor($money * Constant::STORE_PROVINCE_AGENT);
            $city_agent->increment("useable_money", $city_agent_money);
            $city_agent->increment("total_money", $city_agent_money);
            //市级代理
            $city_agent_income_log = new CityAgentIncomeLog();
            $city_agent_income_log->city_agent_id = $city_agent->id;
            $city_agent_income_log->store_cash_id = $store_cash->id;
            $city_agent_income_log->store_id = $store->id;
            $city_agent_income_log->money = $city_agent_money;
            $city_agent_income_log->proportion = Constant::STORE_PROVINCE_AGENT;
            $city_agent_income_log->save();
        }


        // todo 平台提成 50%

        $platform_income_money = floor($money * Constant::PLATFORM_INCOME); // 平台获得金额

        // todo  平台公司 50%
        $company_income_money = floor($platform_income_money * Constant::PLATFORM_COMPANY_INCOME);
        $company_income = CompanyIncome::whereId(1)->first();
        $company_income->increment("total_money", $company_income_money);
        $company_income->increment("usable_money", $company_income_money);
        //平台公司收益日志
        $company_income_log = new CompanyIncomeLog;
        $company_income_log->store_cash_id = $store_cash->id;
        $company_income_log->store_id = $store->id;
        $company_income_log->money = $company_income_money;
        $company_income_log->proportion = Constant::PLATFORM_INCOME;
        $company_income_log->save();

        // todo 内部员工 30%
        $staff_total_income_money = floor($platform_income_money * Constant::PLATFORM_COMPANY_STAFF);
        $staff_total_income = StaffTotalIncome::whereId(1)->first();
        $staff_total_income->increment("total_money", $staff_total_income_money);
        $staff_total_income->increment("useable_money", $staff_total_income_money);
        // 日志记录
        $staff_income_log = new StaffIncomeLog();
        $staff_income_log->store_id = $store->id;
        $staff_income_log->store_cash_id = $store_cash->id;
        $staff_income_log->money = $staff_total_income_money;
        $staff_income_log->proportion = Constant::PLATFORM_COMPANY_STAFF;
        $staff_income_log->save();

        // todo 商家推荐人收入 20%
        $member_recommend_store = \DB::table("member_recommend_store")->where("store_id", $store->id)->first();
        if (isset($member_recommend_store )){
            $member_money = floor($platform_income_money * Constant::PLATFORM_RECOMMEND_MEMBER);
            $member = Member::whereId($member_recommend_store->member_id)->first();
            if (isset($member)){
                $member->increment("useable_bonus_money", $member_money);
                $member->increment("total_bonus_money", $member_money);
                // 日志记录
                $member_recommend_income_log = new MemberRecommendIncomeLog();
                $member_recommend_income_log->member_id = $member->id;
                $member_recommend_income_log->money = $member_money;
                $member_recommend_income_log->store_cash_id = $store_cash->id;
                $member_recommend_income_log->store_id = $store_cash->store_id;
                $member_recommend_income_log->proportion = Constant::PLATFORM_RECOMMEND_MEMBER;
                $member_recommend_income_log->save();
            }
        }

        // 比例分配完成
        $store->decrement("usable_money", $store_cash->money);


    }


}
